import axios from "axios";
import { getToken } from "./auth";

// 将来使用请求的话前缀都是https://2208aaa.lanlianhua.work  http://lanlianhua.work:3009
//http://169.254.186.186:3009  http://192.168.7.45:3009
const instance = axios.create({
  baseURL: "http://lanlianhua.work:3009",
  // baseURL: "http://192.168.7.45:3009",
  timeout: 5000, // 请求超时时间
});
// 全局拦截（请求拦截，响应拦截）
// 全局请求拦截
instance.interceptors.request.use(
  function (config) {
    if (getToken()) {
      config.headers.authorization = `Bearer ${getToken()}`;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

// 全局响应拦截
instance.interceptors.response.use(
  function (response) {
    return response.data;
  },
  function (error) {
    return Promise.reject(error);
  }
);
// 封装get，post，del，put请求
export const get = (url, params) =>
  instance.get(url, {
    params,
  }); // 请求头
export const post = (url, data) => instance.post(url, data); // 请求体
export const del = (url, params) =>
  instance.delete(url, {
    params,
  });
export const put = (url, data) => instance.put(url, data); // 请求体
export default instance;
